﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Kinect;



namespace command_interface
{
    

    public partial class Form1 : Form
    {

        
        
        //TODO: stop the key spam when holding button down
        //      Fix button names
        //      Refactor code


        enum theDirections { north, north_east, north_west, east, west, south, south_east, south_west, stop };
        enum w_s { w, s, none };
        enum a_d { a, d, none };
        w_s ws;
        a_d ad;
        SerialPort serialPort = new SerialPort();
        public bool allowWASD = false;
        StringBuilder receivedOverSerial = new StringBuilder();


        public Form1()
        {
            InitializeComponent();


            refreshPorts();
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.keyDown);
            this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.keyUp);
            ws = w_s.none;
            ad = a_d.none;
            button12.Enabled = false;
            
        }

        private void refreshPorts()
        {
            portBox.Items.Clear();

            string[] comPorts = SerialPort.GetPortNames();

            foreach (string port in comPorts)
            {
                portBox.Items.Add(port);
            }

            if (comPorts.Length == 1)
            {
                portBox.SelectedIndex = 0;
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            doTheNorth_West();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            doTheNorth();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            doTheNorth_East();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            doTheWest();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            doTheStop();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            doTheEast();
        }

        private void button9_Click(object sender, EventArgs e)
        {

            doTheSouth_West();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            doTheSouth();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            doTheSouth_East();
        }



        private void doTheNorth()
        {
            pictureBox1.Image = Properties.Resources.North;
            serialPort.Write("ww" + '\n');
        }
        private void doTheNorth_East()
        {
            pictureBox1.Image = Properties.Resources.North_East;
            serialPort.Write("wd" + '\n');
        }
        private void doTheNorth_West()
        {
            pictureBox1.Image = Properties.Resources.North_West;
            serialPort.Write("wa" + '\n');
        }
        private void doTheEast()
        {
            pictureBox1.Image = Properties.Resources.East;
            serialPort.Write("dd" + '\n');
        }
        private void doTheWest()
        {
            pictureBox1.Image = Properties.Resources.West;
            serialPort.Write("aa" + '\n');
        }
        private void doTheSouth_West()
        {
            pictureBox1.Image = Properties.Resources.South_West;
            serialPort.Write("sa" + '\n');
        }
        private void doTheSouth_East()
        {
            pictureBox1.Image = Properties.Resources.South_East;
            serialPort.Write("sd" + '\n');
        }

        private void doTheSouth()
        {
            pictureBox1.Image = Properties.Resources.South;
            serialPort.Write("ss" + '\n');
        }
        private void doTheStop()
        {
            pictureBox1.Image = Properties.Resources.Stop;
            serialPort.Write("ff" + '\n');
        }

        private bool keyDownBool = false;
        private void keyDown(object sender, KeyEventArgs e)
        {
            if (keyDownBool)
            {
                e.SuppressKeyPress = true;
            }
            keyDownBool = true;
            
            switch (e.KeyData)
            {
                case Keys.W:
                    ws = w_s.w;
                    break;
                case Keys.S:
                    ws = w_s.s;
                    break;

                case Keys.A:
                    ad = a_d.a;
                    break;

                case Keys.D:
                    ad = a_d.d;
                    break;

                case Keys.F:
                    ad = a_d.none;
                    ws = w_s.none;
                    break;
                default:
                    break;
            }
     

            GetState();
        }
        
        private void keyUp(object sender, KeyEventArgs e)
        {
            keyDownBool = false;
            switch (e.KeyData)
            {
                case Keys.W:
                    if (ws == w_s.w)
                        ws = w_s.none;
                    break;
                case Keys.S:
                    if (ws == w_s.s)
                        ws = w_s.none;
                    break;

                case Keys.A:
                    if (ad == a_d.a)
                        ad = a_d.none;
                    break;

                case Keys.D:
                    if (ad == a_d.d)
                        ad = a_d.none;
                    break;

                case Keys.F:
                    ad = a_d.none;
                    ws = w_s.none;
                    break;
                default:
                    break;
            }


            GetState();
        }

        private void SendCommand(theDirections input)
        {

            //add serial code here but don't replace the functions calling the images: don't disturb the art
            switch (input)
            {

                case (theDirections.north):
                    doTheNorth();
                    break;
                case (theDirections.south):
                    doTheSouth();
                    break;
                case (theDirections.east):
                    doTheEast();
                    break;
                case (theDirections.west):
                    doTheWest();
                    break;
                case (theDirections.north_east):
                    doTheNorth_East();
                    break;
                case (theDirections.north_west):
                    doTheNorth_West();
                    break;
                case (theDirections.south_east):
                    doTheSouth_East();
                    break;
                case (theDirections.south_west):
                    doTheSouth_West();
                    break;
                case (theDirections.stop):
                    doTheStop();
                    break;
                default:
                    break;
            }

        }
        

        private void GetState()
        {
            if (allowWASD == true)
            {
                switch (ws)
                {
                    case (w_s.none):

                        switch (ad)
                        {
                            case (a_d.none):
                                SendCommand(theDirections.stop);
                                break;
                            case (a_d.d):
                                SendCommand(theDirections.east);
                                break;

                            case (a_d.a):
                                SendCommand(theDirections.west);
                                break;

                        }
                        break;
                    case (w_s.s):
                        switch (ad)
                        {
                            case (a_d.none):
                                SendCommand(theDirections.south);
                                break;
                            case (a_d.d):
                                SendCommand(theDirections.south_east);
                                break;

                            case (a_d.a):
                                SendCommand(theDirections.south_west);
                                break;

                        }

                        break;
                    case (w_s.w):
                        switch (ad)
                        {
                            case (a_d.none):
                                SendCommand(theDirections.north);
                                break;
                            case (a_d.d):
                                SendCommand(theDirections.north_east);
                                break;

                            case (a_d.a):
                                SendCommand(theDirections.north_west);
                                break;
                        }

                        break;
                    default:
                        break;
                }
            }


        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            ToggleDiretionButtons(false);
            SendCommand(theDirections.stop);
            try
            {
                serialPort.Close();
                button12.Enabled = false;
                button11.Enabled = true;
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void selectButton_Click(object sender, EventArgs e)
        {

            
            try
            {
                serialPort.PortName = portBox.Text;
                serialPort.BaudRate = 9600;
                serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                serialPort.Open();
                ToggleDiretionButtons(true);
                button12.Enabled = true;
                button11.Enabled = false;
              
            }

            catch (Exception ex)
            {
              //  MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


        }

        private void refreshButton_Click(object sender, EventArgs e)
        {
            refreshPorts();
        }

        private void ToggleDiretionButtons(bool toggle)
        {
            button1.Enabled = toggle;
            button2.Enabled = toggle;
            button3.Enabled = toggle;
            button4.Enabled = toggle;
            button5.Enabled = toggle;
            button6.Enabled = toggle;
            button7.Enabled = toggle;
            button8.Enabled = toggle;
            button9.Enabled = toggle;
            allowWASD = toggle;
        }

        private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            receivedOverSerial.Append(sp.ReadExisting());
            Invoke(new Action(() => richTextBox1.AppendText(receivedOverSerial.ToString())));
        }

        private void buttonClearReceived_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //call shit from Kinect class here maybe?


        }

        private void colorSensorStartButton_Click(object sender, EventArgs e)
        {

            Window window = new Window
            {
                Title = "Kinect Stream",
                Content = new UserControl1()
            };
            window.Show();
            
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
        }

    }
}
